package com.chuanyang.driverh.mainstart.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chuanyang.driverh.R;

import java.util.List;


/***
 *
 * @author fusl 2015-10-15
 *
 */
public class AdPagerAdapter extends PagerAdapter {

	private Context mContext;
	private List<AdViewPager> mList;
	public AdPagerAdapter(Context context, List<AdViewPager> list) {
		super();
		mContext = context;
		mList = list;
	}

	/***
	 * 返回多少page
	 * @return
	 */
	@Override
	public int getCount() {
		return Integer.MAX_VALUE;//伪无限循环，将返回值变得特别大
	}

	/***
	 *
	 * @param view
	 * @param object
	 * @return  true:表示不去创建，使用缓存 false:去重新创建
	 */
	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	/***
	 * 销毁page
	 * @param container
	 * @param position 当前销毁第几个page
	 * @param object 当前销毁的page对象
	 */
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	/***
	 * 类似于BaseAdapter的getView
	 * 由于最多3个界面，所有不用viewHolder
	 * @param container
	 * @param position
	 * @return
	 */
	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		View view = View.inflate(mContext, R.layout.layout_ad_pager_change, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.ad_pager_iv);
		imageView.setImageResource(mList.get(position % mList.size()).getIconResId());
		imageView.setTag(position);
		container.addView(view);//一定不能少将view加入到ViewPager中
		return view;
	}
}
